package file

import (
	"context"
	"git.mycaigou.com/gfyx/common/usermetadata"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/repository/common/file"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/common"
	"time"
)

type fileService struct {
	fileRepo file.FileRepoIface
}

func NewFileService(fileRepo file.FileRepoIface) FileServiceIface {
	return &fileService{
		fileRepo: fileRepo,
	}
}

func (s *fileService) SaveFile(ctx context.Context, po *pb.SaveToFileRequest) (int64, error) {
	curUser := usermetadata.GetMetaUserInfo(ctx)
	newFilePo, err := s.fileRepo.SaveFile(ctx, &file.GfyxFilesPo{
		Id:         0,
		Business:   po.Business,
		FileType:   po.FileType,
		FileName:   po.FileName,
		FilePath:   po.FilePath,
		FileSize:   po.FileSize,
		IsDeleted:  constants.IsDeletedFalse,
		CreatedOn:  time.Now(),
		CreatedBy:  curUser.TenantUserUid,
		ModifiedOn: time.Now(),
		ModifiedBy: curUser.TenantUserUid,
	})
	if err != nil {
		return 0, err
	}

	if newFilePo == nil {
		return 0, nil
	}

	return newFilePo.Id, nil
}

func (s *fileService) GetFileInfo(ctx context.Context, fileId int64) (*file.GfyxFilesPo, error) {
	return s.fileRepo.QueryById(ctx, fileId)
}
